﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TCCHWLib.Commands
{
    public class TxReadCommand: ICommand
    {
        public TxReadCommand(object parameter1, ParameterType paramterType1,object index,ParameterType parameterType3)
        {
            m_Operand1 = parameter1;
            m_Operand3 = index;
            m_Type1 = paramterType1;
            m_Type3 = parameterType3;
            m_ExOperand1 = parameter1;
            m_ExOperand3 = index;
            m_ExType1 = paramterType1;
            m_ExType3 = parameterType3;
            this.Value = false;
        }

        public override object Execute(Processor processor)
        {
            int address = Convert.ToInt32(m_ExOperand1);
            int index = Convert.ToInt32(m_ExOperand3);
            address += index;
            if (processor.WriteSignatureRegister.LookUp(address))
            {
                try
                {
                    m_Value = processor.WriteSet[address];
                    processor.Cache.MesiModified(address);
                    return m_Value;
                }
                catch (Exception)
                {
                }
            }
            processor.ReadSignatureRegister.Insert(address);
            m_Value = processor.Cache.Read(address);
            return m_Value;
        }

        public override string ToString()
        {
            return String.Format("TX-READ ( {0}{1} )", m_Operand1, ((m_Operand3 != null) ? " + " + m_Operand3 : ""));
            //return base.ToString() + String.Format(" > ADDR={0}, VAL={1} , next pc {2}", m_Operand1, m_Operand2, NextPC);
        }
    }
}
